package com.cn.serverline.lineicp.workshopPlan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.serverline.lineicp.workshopPlan.entity.AuxiliaryMaterials;
import com.cn.serverline.lineicp.workshopPlan.entity.TypesOfConsumables;
import com.cn.serverline.lineicp.workshopPlan.entity.request.AuxiliaryMaterialsRequest;
import com.cn.serverline.lineicp.workshopPlan.enums.RolingPlanException;
import com.cn.serverline.lineicp.workshopPlan.mapper.AuxiliaryMaterialsMapper;
import com.cn.serverline.lineicp.workshopPlan.mapper.TypesOfConsumablesMapper;
import com.cn.serverline.lineicp.workshopPlan.service.AuxiliaryMaterialsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 辅助材料服务实现类
 *
 */

@Service
public class AuxiliaryMaterialsServiceImpl extends ServiceImpl<AuxiliaryMaterialsMapper, AuxiliaryMaterials> implements AuxiliaryMaterialsService {

    @Resource
    private AuxiliaryMaterialsMapper auxiliaryMaterialsMapper;
    /**
     * 添加辅助材料
     * @param auxiliaryMaterialsRequest 请求参数
     * @return
     */
    @Override
    public Long add(AuxiliaryMaterialsRequest auxiliaryMaterialsRequest) {

        //判断编号是否重复
        LambdaQueryWrapper<AuxiliaryMaterials> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AuxiliaryMaterials::getCode, auxiliaryMaterialsRequest.getCode());
        AuxiliaryMaterials existingSparePart = auxiliaryMaterialsMapper.selectOne(queryWrapper);
        if (existingSparePart != null) {
            // 如果编号已存在，抛出异常或返回错误信息
            throw new ServiceException(RolingPlanException.ONE);
        }

        AuxiliaryMaterials spareParts = new AuxiliaryMaterials();
        BeanUtil.copyProperties(auxiliaryMaterialsRequest, spareParts);
        spareParts.setCreateTime(new Date());
        this.save(spareParts);
        //返回新创建的SparePartsType实例的id，作为一个字符串。
        // getId()方法假设返回备件的唯一标识符，toString()将其转换为字符串表示。
        return spareParts.getId();
    }

    /**
     *删除辅助材料
     * @param auxiliaryMaterialsRequest 请求参数
     */
    @Override
    public void del(AuxiliaryMaterialsRequest auxiliaryMaterialsRequest) {
        this.removeById(auxiliaryMaterialsRequest.getId());
    }

    /**
     * 修改辅助材料
     * @param auxiliaryMaterialsRequest 请求参数
     */
    @Override
    public void edit(AuxiliaryMaterialsRequest auxiliaryMaterialsRequest) {

        //判断编号是否重复
        LambdaQueryWrapper<AuxiliaryMaterials> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AuxiliaryMaterials::getCode, auxiliaryMaterialsRequest.getCode());
        AuxiliaryMaterials existingSparePart = auxiliaryMaterialsMapper.selectOne(queryWrapper);
        if (existingSparePart != null) {
            // 如果编号已存在，抛出异常或返回错误信息
            throw new ServiceException(RolingPlanException.ONE);
        }
        // 首先，我们需要根据请求中的ID找到要修改的零组件记录
        AuxiliaryMaterials auxiliaryMaterials = this.getById(auxiliaryMaterialsRequest.getId());

        // 如果记录不存在，则抛出异常或返回错误信息
        if (auxiliaryMaterials == null) {
            throw new ServiceException(RolingPlanException.ZERO);
        }

        // 使用BeanUtil.copyProperties方法将请求参数中的属性复制到找到的零组件记录中
        BeanUtil.copyProperties(auxiliaryMaterialsRequest, auxiliaryMaterials);

        // 更新修改时间
        auxiliaryMaterials.setUpdateTime(new Date());

        // 调用save方法来持久化更改
        this.updateById(auxiliaryMaterials);
    }

    /**
     * 查询辅助材料
     * @param auxiliaryMaterialsRequest 请求参数
     * @return 匹配的零组件列表
     */
    @Override
    public Page<AuxiliaryMaterials> findList(AuxiliaryMaterialsRequest auxiliaryMaterialsRequest) {
        // 创建查询条件构建器
        LambdaQueryWrapper<AuxiliaryMaterials> queryWrapper = new LambdaQueryWrapper<>();

        // 根据请求参数构建查询条件
        queryWrapper.like(auxiliaryMaterialsRequest.getName() != null, AuxiliaryMaterials::getName, auxiliaryMaterialsRequest.getName());
        queryWrapper.eq(auxiliaryMaterialsRequest.getId() != null, AuxiliaryMaterials::getId, auxiliaryMaterialsRequest.getId());

        // 返回查询结果
        return this.page(PageFactory.defaultPage(), queryWrapper);
    }
}
